home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / snip9611.zip / NLCNVRT.C < prev    next >
C/C++ Source or Header  |  1996-11-24  |  3KB  |  137 lines

  1. /* +++Date last modified: 02-Nov-1995 */
  2.  
  3. /*
  4. **  NLCNVRT.C - A utility to convert newlines in text files
  5. **
  6. **  public domain by Bob Stout
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "errors.h"
  13. #include "sniptype.h"
  14.  
  15. FILE *infile;
  16. FILE *outfile;
  17.  
  18. void usage(void);
  19. void toDOS(void);
  20. void toUnix(void);
  21. void fromKB(void);
  22. void Cout(int ch, FILE *outfile);
  23. void Sout(char *str, FILE *outfile);
  24.  
  25. main(int argc, char *argv[])
  26. {
  27.       char outname[L_tmpnam] = "";
  28.  
  29.       if (argc < 3)
  30.             usage();
  31.       if (NULL == strchr("-/", argv[1][0]))
  32.             usage();
  33.  
  34.       infile = cant(argv[2], "rb");
  35.  
  36.       if (3 == argc || NULL == (outfile = fopen(argv[3], "wb")))
  37.       {
  38.             tmpnam(outname);
  39.             outfile = cant(outname, "wb");
  40.       }
  41.  
  42.       switch (argv[1][1])
  43.       {
  44.       case 'd':
  45.       case 'D':
  46.             toDOS();
  47.             break;
  48.  
  49.       case 'u':
  50.       case 'U':
  51.             toUnix();
  52.             break;
  53.  
  54.       case 'k':
  55.       case 'K':
  56.             fromKB();
  57.             break;
  58.  
  59.       default:
  60.             usage();
  61.       }
  62.       fclose(infile);
  63.       fclose(outfile);
  64.       if (*outname)
  65.       {
  66.             if (Success_ != remove(argv[2]))
  67.             {
  68.                   remove(outname);
  69.                   ErrExit("Can't remove %s", argv[2]);
  70.             }
  71.             rename(outname, argv[2]);
  72.       }
  73.       return EXIT_SUCCESS;
  74. }
  75.  
  76. void usage(void)
  77. {
  78.       puts("Usage: NLCNVRT -[d | u | k] infile [outfile]");
  79.       puts("Switches: -d  Convert to DOS - converts LF to CRLF");
  80.       puts("        : -u  Convert to Unix - converts CRLF to LF");
  81.       puts("        : -k  Convert to DOS from KB - converts CR to CRLF");
  82.       exit(EXIT_FAILURE);
  83. }
  84.  
  85. void toDOS(void)
  86. {
  87.       int ch, lastch = 0;
  88.  
  89.       while (EOF != (ch = fgetc(infile)))
  90.       {
  91.             if ('\n' == ch && '\r' != lastch)
  92.                   fputc('\r', outfile);
  93.             fputc(lastch = ch, outfile);
  94.       }
  95.       if ('\n' != lastch)
  96.             fputs("\r\n", outfile);
  97. }
  98.  
  99. void toUnix(void)
  100. {
  101.       int ch, lastch = 0;
  102.  
  103.       while (EOF != (ch = fgetc(infile)) && '\x1a' != ch)
  104.       {
  105.             if ('\r' != ch)
  106.                   fputc(lastch = ch, outfile);
  107.       }
  108.       if ('\n' != lastch)
  109.             fputc('\n', outfile);
  110. }
  111.  
  112. void fromKB(void)
  113. {
  114.       int ch, lastch = 0;
  115.  
  116.       while (EOF != (ch = fgetc(infile)))
  117.       {
  118.             if ('\r' == ch)
  119.                   fputc('\n', outfile);
  120.             fputc(lastch = ch, outfile);
  121.       }
  122.       if ('\r' != lastch)
  123.             fputs("\r\n", outfile);
  124. }
  125.  
  126. void Cout(int ch, FILE *outfile)
  127. {
  128.       if (EOF == fputc(ch, outfile))
  129.             exit(EXIT_FAILURE);
  130. }
  131.  
  132. void Sout(char *str, FILE *outfile)
  133. {
  134.       if (EOF == fputs(str, outfile))
  135.             exit(EXIT_FAILURE);
  136. }
  137.